﻿
def f(x):
	return x*x

print "map(f,[1,2,3,4,5]) =",map(f,[1,2,3,4,5])

print "map(str,[1,2,3,4,5]) =",map(str,[1,2,3,4,5])

def add(x,y):
	return x+y

# reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

print "add(3,5) =",add(3,5)
print "reduce(add,[1,3,5,7,9]) =",reduce(add,[1,3,5,7,9])

def fn(x,y):
	return x*10+y
	
print "reduce(fn,[1,3,5,7,9]) =",reduce(fn,[1,3,5,7,9])

def char2num(s):
	return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
	
print "reduce(fn,map(char2num,'13579')) =",reduce(fn,map(char2num,'13579'))

def str2int(s):
	def fn(x,y):
		return x*10+y
	def char2num(s):
		return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
	return reduce(fn,map(char2num,'13579'))

print "str2int('13579') =",str2int('13579')

def str2int_2(s):
	return reduce(lambda x,y:x*10+y,map(char2num,s))
	
print "str2int_2('13579') =",str2int_2('13579')

print "practice------------------------------------------------------"

def firstCharCap(s):
	if isinstance(s,str) and len(s)>0:
		return s[0].upper()+s[1:].lower()
	return s
L=['adam', 'LISA', 'barT']
print "L =",L
print "map(firstCharCap,L) =",map(firstCharCap,L)
